<?php

namespace App\Admin\Controllers\Combination;

use App\Admin\Repositories\CmcPromotionPintuanGroup;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB as CmcGoods;

class CmcPromotionPintuanGroupController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CmcPromotionPintuanGroup(), function (Grid $grid) {
            $grid->column('group_id')->sortable();
            $grid->column('site_id');
            $grid->column('goods_id');
            $grid->column('is_virtual_goods', '是否虚拟商品')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'不虚拟',
                    1=>'虚拟'
                ])
            );
            $grid->column('pintuan_id');
            $grid->column('head_id');
            $grid->column('pintuan_num');
            $grid->column('pintuan_count');
            $grid->column('end_time');
            $grid->column('status');
            $grid->column('status', '拼团当前状态')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'未支付',
                    1=>'拼团失败',
                    2=>'组团中',
                    3=>'拼团成功'
                ])
            );
            $grid->column('is_virtual_buy');
            $grid->column('is_virtual_buy', '是否虚拟成团')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'不虚拟',
                    1=>'虚拟'
                ])
            );
            $grid->column('is_single_buy');
            $grid->column('is_single_buy', '是否单独购买')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'不单独',
                    1=>'单独'
                ])
            );
            $grid->column('is_promotion');
            $grid->column('is_promotion', '是否团长优惠')->switch()->filter(
                Grid\Column\Filter\In::make([
                    0=>'不是',
                    1=>'是'
                ])
            );
            $grid->column('buy_num');
            $grid->column('head_member_img')->image($server=null,80,80);
            $grid->column('head_nickname')->editable(true);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            // 倒序排序
            $grid->model()->orderBy('created_at', 'desc');
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->equal('head_nickname','组长会员昵称');
                $filter->equal('group_id','拼团ID');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CmcPromotionPintuanGroup(), function (Show $show) {
            $show->field('group_id');
            $show->field('site_id');
            $show->field('goods_id');
            $show->field('is_virtual_goods');
            $show->field('pintuan_id');
            $show->field('head_id');
            $show->field('pintuan_num');
            $show->field('pintuan_count');
            $show->field('end_time');
            $show->field('status');
            $show->field('is_virtual_buy');
            $show->field('is_single_buy');
            $show->field('is_promotion');
            $show->field('buy_num');
            $show->field('head_member_img');
            $show->field('head_nickname');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CmcPromotionPintuanGroup(), function (Form $form) {
            $form->display('group_id');
            $form->text('site_id');
            $form->select('goods_id')->options(
                CmcGoods::table('cmc_goods')->where('goods_state',1)->pluck('goods_id','goods_id')
            );
            $form->text('is_virtual_goods');
            $form->text('pintuan_id');
            $form->text('head_id');
            $form->text('pintuan_num');
            $form->text('pintuan_count');
            $form->datetime('end_time');
            $form->text('status');
            $form->text('is_virtual_buy');
            $form->text('is_single_buy');
            $form->text('is_promotion');
            $form->text('buy_num');
            $form->image('head_member_img');
            $form->text('head_nickname');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
